# @Author: richard
# @Date:   2017-11-10 17:36:27
# @Last Modified by:   richard
# @Last Modified time: 2017-11-21 10:41:01
# Purpose: Install tengine

{% set Version = "tengine-2.2.2" %}
{% if salt['file.directory_exists']('/usr/local/nginx') %}
{% set NginxN = "Nginx1" %}
{% else %}
{% set NginxN = "nginx" %}
{% endif %}

include:
    - lua.install
    - user.www

pkg-install-pre:
    pkg.installed:
        - names:
            - zlib
            - zlib-devel
            - openssl
            - pcre

ajp-module-install:
    file.managed:
        - name: /usr/local/src/nginx_ajp_module-master.zip
        - source: salt://nginx/files/nginx_ajp_module-master.zip
        - user: root
        - group: root
        - mode: 755
        - unless: test -f /usr/local/src/nginx_ajp_module-master.zip
    cmd.run:
        - name: cd /usr/local/src/ && unzip -o nginx_ajp_module-master.zip 

nginx-source-install:
    file.managed:
        - name: /usr/local/src/{{Version}}.tar.gz
        - source: salt://nginx/files/{{Version}}.tar.gz
        - user: root
        - group: root
        - mode: 755
        - unless: test -f /usr/local/src/{{Version}}.tar.gz

    cmd.run:
        - name: cd /usr/local/src && tar -zxf {{Version}}.tar.gz && cd {{Version}} && export LUAJIT_LIB=/usr/local/luajit/lib && export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0 && ./configure --user=www --group=www --prefix=/usr/local/tengine --add-module=/usr/local/src/ngx_devel_kit-0.2.19 --add-module=/usr/local/src/lua-nginx-module-0.9.16 --with-http_ssl_module --with-file-aio   --with-pcre  --add-module=/usr/local/src/nginx_ajp_module-master && make 1> /dev/null && make install 1> /dev/null && chown -R www:www  /usr/local/tengine 
        - unless: test -d /usr/local/{{NginxN}}
        - require:
            - file: nginx-source-install
            - cmd: luaJIT-source-install
            - cmd: kit-module-install
            - cmd: lua-module-install
            - cmd: ajp-module-install

nginx-file-chmod:
    file.directory:
        - name: /usr/local/tengine/conf/vhost
        - user: www
        - group: www
        - mode: 644
        - onlyif: test -d /usr/local/tengine/conf

    cmd.run:
        - name: cd /usr/local/tengine/sbin/ && chown root:www nginx && chmod u+s nginx && ln -s /usr/local/tengine /usr/local/nginx
        - unless: test -d /usr/local/nginx

nginx-init:
    file.managed:
        - name: /etc/init.d/nginx
        - source: salt://nginx/files/nginx-init
        - unless: test -f /etc/init.d/nginx
        - mode: 755
        - user: root
        - group: root

    cmd.run:
        - name: chkconfig --add nginx
        - unless: chkconfig --list |grep nginx
        - require:
            - file: nginx-init


/usr/local/{{NginxN}}/conf/nginx.conf:
    file.managed:
        - source: salt://nginx/files/nginx.conf
        - user: www
        - group: www
        - mode: 644
        - template: jinja
        - defaults:
            nginx_user: www
            defaults_port: 18118
            num_cpus: {{ grains['num_cpus'] }}
        - require:
            - cmd: nginx-source-install

    cmd.run:
        - name: ldconfig

